# -*- coding: utf-8 -*-
"""
Created on Fri Mar 26 10:59:14 2021

@author: 雁痕
"""

from blockchain import Blockchain
GENESIS = '00000000-0000-0000-0000-000000000000'

class Ledger(Blockchain):
    
    def balance(self,account):
        b = 0
        for i in self.blocks:
            for j in i.items:
                if j.sender == account:
                    b -= j.amount
                elif j.receiver == account:
                    b += j.amount
                    
        return b
                
    def add(self,block):
        for i in block.items:
            assert i.sender == GENESIS or self.balance(i.sender) >= i.amount
            
        super().add(block)
        
        
